//
//  CXDatePicker.m
//  各种自定义小功能
//
//  Created by 蔡翔 on 16/8/2.
//  Copyright © 2016年 蔡翔. All rights reserved.
//

#import "CXDatePicker.h"
#import "CXDatePickerSheetView.h"

#define CX_screenWidth [UIScreen mainScreen].bounds.size.width
#define CX_screenHeight [UIScreen mainScreen].bounds.size.height
#define CXPickerSheetViewHeight CX_screenHeight * 0.4

@interface CXDatePicker ()
{
    NSString* _titleString;
}

@property (nonatomic, strong) CXDatePickerSheetView *datePickerSheetView;
/** 遮盖 */
@property (nonatomic, strong) UIButton *coverView;

@property (nonatomic, copy) DataTimeSelect selectBlock;

@end

@implementation CXDatePicker

/** 动画时间 */
static CGFloat const CX_animateWithDuration = 0.25;

- (instancetype)initWithTopTitle:(NSString *)title{
    
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, CX_screenWidth, CX_screenHeight);
        [[[UIApplication sharedApplication] keyWindow] addSubview:self];
        // 遮盖
        self.coverView = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _coverView.backgroundColor = [UIColor blackColor];
        _coverView.alpha = 0.0;
        [_coverView addTarget:self action:@selector(dismissPickerView) forControlEvents:UIControlEventTouchUpInside];
        _coverView.frame = CGRectMake(0, 0, CX_screenWidth, CX_screenHeight);
        [self addSubview:self.coverView];
        if (title) {
            _titleString = title;
        }
        [self setupDatePickerSheetView];
    }
    return self;
}

- (BOOL)anySubViewScrolling:(UIView *)view{
    if ([view isKindOfClass:[UIScrollView class]]) {
        UIScrollView *scrollView = (UIScrollView *)view;
        if (scrollView.dragging || scrollView.decelerating) {
            return YES;
        }
    }
    for (UIView *theSubView in view.subviews) {
        if ([self anySubViewScrolling:theSubView]) {
            return YES;
        }
    }
    return NO;
}

#pragma mark - - - 按钮的点击事件
- (void)sureBtnClick {
    if (![self anySubViewScrolling:self.datePickerSheetView]) {
        !_selectDate?:_selectBlock([self secondsGMTForDate:_selectDate],@"");
        [self dismissPickerView];
    }
}
#pragma mark - 立即按钮
- (void)ImmediatelyBtnClick{
    !_selectDate?:_selectBlock([self secondsGMTForDate:[NSDate date]],_titleString);
    [self dismissPickerView];
}

//DatePicker值改变
- (void)datePickerValueChange:(id)sender {
    _selectDate = [sender date];
}

// 消失
- (void)dismissPickerView {
    [UIView animateWithDuration:CX_animateWithDuration animations:^{
        self.datePickerSheetView.transform = CGAffineTransformMakeTranslation(0, CXPickerSheetViewHeight);
        self.coverView.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self.datePickerSheetView removeFromSuperview];
        [self.coverView removeFromSuperview];
        [self removeFromSuperview];
    }];
}

// 出现
- (void)show {
    [UIView animateWithDuration:CX_animateWithDuration animations:^{
        self.datePickerSheetView.transform = CGAffineTransformMakeTranslation(0, - CXPickerSheetViewHeight);
        self.coverView.alpha = 0.3;
    }];
}

- (void)setupDatePickerSheetView {
    self.datePickerSheetView = [[[NSBundle mainBundle] loadNibNamed:@"CXDatePickerSheetView" owner:nil options:nil] firstObject];
    _datePickerSheetView.frame = CGRectMake(0, CX_screenHeight, CX_screenWidth, CXPickerSheetViewHeight);
    [_datePickerSheetView addTargetCancelBtn:self action:@selector(dismissPickerView)];
    [_datePickerSheetView addTargetSureBtn:self action:@selector(sureBtnClick)];
    if (_titleString) {
        [_datePickerSheetView addTargetImmediatelyBtn:self action:@selector(ImmediatelyBtnClick)];
        [_datePickerSheetView.immediatelyButton setTitle:_titleString forState:UIControlStateNormal];
    }
    
    [_datePickerSheetView.datePicker addTarget:self action:@selector(datePickerValueChange:) forControlEvents:UIControlEventValueChanged];
    //DatePicker属性设置
    _selectDate = [[NSDate alloc] init];
    _datePickerSheetView.datePicker.date = _selectDate;
    _datePickerSheetView.datePicker.minimumDate = _selectDate;
    _datePickerSheetView.datePicker.datePickerMode = UIDatePickerModeDateAndTime;
    [self addSubview:_datePickerSheetView];
}

- (void)animationWithView:(UIView *)view duration:(CFTimeInterval)duration{
    CAKeyframeAnimation * animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = duration;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    NSMutableArray *values_Arr = [NSMutableArray array];
    [values_Arr addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values_Arr addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
    [values_Arr addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values_Arr;
    animation.timingFunction = [CAMediaTimingFunction functionWithName: @"easeInEaseOut"];
    [view.layer addAnimation:animation forKey:nil];
}

//转换8小时问题(抽成分类 做转换!)
- (NSDate *)secondsGMTForDate:(NSDate *)date {
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    return [date  dateByAddingTimeInterval: interval];
}

#pragma mark - - - setter
- (void)setSelectDate:(NSDate *)selectDate {
    _selectDate = selectDate;
    if (selectDate) {
        _datePickerSheetView.datePicker.date = selectDate;
    }
}
- (void)setDatePickerMode:(UIDatePickerMode)datePickerMode {
    _datePickerSheetView.datePicker.datePickerMode = datePickerMode;
}

- (void)setIsBeforeTime:(BOOL)isBeforeTime {
    if (isBeforeTime) {
        [_datePickerSheetView.datePicker setMinimumDate:[NSDate dateWithTimeIntervalSince1970:0]];
    } else {
        [_datePickerSheetView.datePicker setMinimumDate:[NSDate date]];
    }
}

- (void)setMinSelectDate:(NSDate *)minSelectDate {
    if (minSelectDate) {
        [_datePickerSheetView.datePicker setMinimumDate:minSelectDate];
    }
}

- (void)setMaxSelectDate:(NSDate *)maxSelectDate {
    if (maxSelectDate) {
        [_datePickerSheetView.datePicker setMaximumDate:maxSelectDate];
    }
}

- (void)didFinishSelectedDate:(DataTimeSelect)selectDataTime {
    _selectBlock = selectDataTime;
}

@end


